package js.android.nateroidbeta;

import java.util.ArrayList;

import rath.nateon.NateUser;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class ChatScreen extends Activity {
	
	private Button btnSend;
	private EditText etChat;
	private TextView tvUserName;
	private ListView lstLogChat;
	private ArrayList<ChatRecord> log_chatlist;
	private final Handler mHandler = new Handler();
	private final Runnable mUpdateResults = new Runnable(){
		public void run()
		{
			UpdateChatLog();
		}
	};
	
	private final NateUser partner = NateOnSingleton.getPartner();
	private final String MyDisplayName = getDisplayName(NateOnSingleton.getOwner());
	private final String PartnerDisplayName = getDisplayName(partner);
	
			
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.chat);		
		 	 
		 btnSend = (Button)findViewById(R.id.btn_send);
	     etChat = (EditText)findViewById(R.id.txt_chat);
	     lstLogChat = (ListView)findViewById(R.id.log_chat);
	     tvUserName = (TextView)findViewById(R.id.lbl_buddy);
	     
		 tvUserName.setText(PartnerDisplayName);
		 log_chatlist = NateOnSingleton.getCurChatLog();
		 lstLogChat.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
		 
		 NateOnSingleton.setChatScreen(this);
 
	        //Set Click Listener
	        btnSend.setOnClickListener(new OnClickListener(){
	        	@Override
	        	public void onClick(View v){
	        		String strChat = etChat.getText().toString();
	        		
	        		NateOnSingleton.SendMessage(strChat);
	        		Add(NateOnSingleton.getEmail() , strChat);
	        		etChat.setText("");
	        	    UpdateChatLog();
	        	}
	        });
	    UpdateChatLog(); 
	 }
	 
	 public void Add(String strWriter, String strTxt)
    {
		 String strDisplay = "";
		 String partnerEmail =partner.getEmail(); 
		if(strWriter.compareTo(partnerEmail) == 0)
		{
			strDisplay = PartnerDisplayName;
		}
		else if(strWriter == NateOnSingleton.getEmail() )
		{
			strDisplay = MyDisplayName; 
		}
		
		ChatRecord chatRecord = new ChatRecord(strDisplay , strTxt );;
		log_chatlist.add(chatRecord);
    }
	 
	 public void MsgFromPeer()
	 {
		 mHandler.post(mUpdateResults);
	 }
	 
    public void UpdateChatLog()
    {
    	try{
    	 lstLogChat.setAdapter(new ArrayAdapter<ChatRecord>(this, android.R.layout.simple_list_item_1, log_chatlist)
				 {
					@Override
					public View getView(int position, View convertView, ViewGroup parent) 
					{
						ChatRecord Entity = getItem(position);
						TextView resultView = new TextView(super.getContext());
						
						try {
							resultView.setText(Entity.m_strWriter + "says :\n   " + Entity.m_strChatLog);
						} catch (Exception e) {
							e.printStackTrace();
						}
						return resultView;
					}
				 });
		 lstLogChat.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    	}catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    }
    
    public String getDisplayName(NateUser user)
    {
    	if(user != null)
    		return user.getName()+":"+user.getNickname();
    	else
    		return null;
    }
    
    public class ChatRecord
    {
    	public String m_strWriter;
    	public String m_strChatLog;
    	// time... blahblah...
    	
    	public ChatRecord(String strWriter, String strChatLog)
    	{
    		m_strWriter = strWriter;
    		m_strChatLog = strChatLog;
    	}
    }
}
